package com.zzptc.zzsgz.bdsjws.utils;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * Created by zzsgz on 2017/4/23.
 */
//普通JAVA类，是没有上下文
public class MyUtil {
    //返回共享首选项对象
    public static SharedPreferences getSharedPre(){
         //获取上下文  再获取共享首选项
        return  MyApplication.getContext().getSharedPreferences("loginnew", Context.MODE_PRIVATE);


    }

    public static boolean isSDCard(){
        String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)){
            return true;
        }else{
            return false;
        }
    }

    //能否读写 能写就写入SD卡，不能就写手机存储中
    public static File getPath(){
        File path = null;
        if(isSDCard()){
            path = Environment.getExternalStorageDirectory();
        }else{
            path = Environment.getDownloadCacheDirectory();
        }

        return path;
    }
    public static Intent getInstallIntent(File file){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");

        return intent;
    }

    //复制文件到手机
    public static void copyFileToMobile(Context context,String fromdbName,String todbName) throws IOException {

        //原始文件位置
        URL url=context.getClass().getClassLoader().getResource("assets/"+fromdbName);
        //目的地文件夹路劲
        String dbfiledir="data/data/"+MyApplication.getContext().getPackageName()+"/databases";

        File fileMobiledir=new File(dbfiledir);
        if(!fileMobiledir.exists()){
            fileMobiledir.mkdirs();
        }

        File dbfileMobile=new File(fileMobiledir+"/"+todbName);
        if(!dbfileMobile.exists()) {
            dbfileMobile.createNewFile();
        }
        FileUtils.copyURLToFile(url,dbfileMobile);
    }


}
